﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DAL;
namespace BLL.TuyenDung
{
    public class ViewHelper
    {
        public static tbl_HS_HoSo Get_HoSoId(int HoSoId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            return db.tbl_HS_HoSos.FirstOrDefault(k => k.HoSoId == HoSoId);
        }
        public static string NoiLamViec_HoSo(int HoSoId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            string NoiLamViec = "";
            try
            {
                var tblHsMucTieuNgheNghiep = db.tbl_HS_MucTieuNgheNghieps.FirstOrDefault(k => k.HoSoId == HoSoId);
                if (tblHsMucTieuNgheNghiep != null)
                {
                    string[] chuoi = tblHsMucTieuNgheNghiep.MoiLamUaThich.Split(';');
                    foreach (var s in chuoi)
                    {
                        NoiLamViec += BLL.Admin.TinhHelper.Get_TinhId(int.Parse(s)).Ten + "; ";
                    }
                }
            }
            catch
            {

            }

            return NoiLamViec;
        }
        public static IQueryable List_HoSoTimKiem(string TuKhoa, int NganhNgheId, int NoiLamViec, int Page, int row)
        {
            bool allNganhNghe = NganhNgheId == -1;
            bool allNoiLam = NoiLamViec == -1;
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");

            var query = db.tbl_HS_HoSos.Where(
                r =>
                r.HoanThanh &&
                (r.TieuDe.Contains(TuKhoa) &&
                 (r.tbl_HS_MucTieuNgheNghieps.First().NganhNghe.Contains(NganhNgheId.ToString()) || allNganhNghe) &&
                 (r.tbl_HS_MucTieuNgheNghieps.First().MoiLamUaThich.Contains(NoiLamViec.ToString()) || allNganhNghe))).
                Select(r => new
                                {
                                    r.HoSoId,
                                    ViTri = r.tbl_HS_MucTieuNgheNghieps.First().ViTriMongMuon,
                                    UserName = r.User.UserName,
                                    KinhNghiem = BLL.NguoiDung.HoSo.CountTotalExp(r.HoSoId),
                                    DiaDiem = NoiLamViec_HoSo(r.HoSoId),
                                    TimeUpDate = r.LastUpdate,
                                });

            return query.Skip(row * (Page - 1)).Take(row);

        }
        public static int CountList_HoSoTimKiem(string TuKhoa, int NganhNgheId, int NoiLamViec)
        {
            bool allNganhNghe = NganhNgheId == -1;
            bool allNoiLam = NoiLamViec == -1;
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");

            var query = db.tbl_HS_HoSos.Where(
                r =>
                r.HoanThanh &&
                (r.TieuDe.Contains(TuKhoa) &&
                 (r.tbl_HS_MucTieuNgheNghieps.First().NganhNghe.Contains(NganhNgheId.ToString()) || allNganhNghe) &&
                 (r.tbl_HS_MucTieuNgheNghieps.First().MoiLamUaThich.Contains(NoiLamViec.ToString()) || allNganhNghe)));


            return query.Count();

        }
        public static void LuHoSoTimKiem(int HosoId, int UserId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var temp = db.tbl_TD_HS_DaLuus.Where(k => k.HoSoId == HosoId && k.UserId == UserId);
            if (temp == null)
            {
                tbl_TD_HS_DaLuu luuHs = new tbl_TD_HS_DaLuu();
                luuHs.HoSoId = HosoId;
                luuHs.UserId = UserId;
                db.tbl_TD_HS_DaLuus.InsertOnSubmit(luuHs);
                db.SubmitChanges();
            }

        }
        public static string DanhSachHocVanTheoHoSo(int HosoId)
        {
            string noidung = "";
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var query = from a in db.tbl_HS_HocVans.Where(k => k.HoSoId == HosoId)
                        select a;
            foreach (var item in query)
            {
                noidung += item.tbl_DM_HocVan.Mota;
            }
            return noidung;
        }
        public static IQueryable List_HoSoDaLuu(int UserId, int Page, int row)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var query = from a in db.tbl_TD_HS_DaLuus.Where(k => k.UserId == UserId)
                        select new
                                   {
                                       a.HoSoId,
                                       HocVan = DanhSachHocVanTheoHoSo(a.HoSoId),
                                       UserName = a.tbl_HS_HoSo.User.UserName,
                                       KinhNghiem = BLL.NguoiDung.HoSo.CountTotalExp(a.HoSoId),
                                       DiaDiem = NoiLamViec_HoSo(a.HoSoId),
                                       TimeUpDate = a.tbl_HS_HoSo.LastUpdate,
                                   };

            return query.Skip(row * (Page - 1)).Take(row);

        }
        public static int CountList_HoSoDaLuu(int UserId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var query = from a in db.tbl_TD_HS_DaLuus.Where(k =>k.UserId == UserId)
                        select a;
            return query.Count();
        }
        public static void Delete_HoSoDaLuu_NTD(int HoSoId,int UserId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var temp = db.tbl_TD_HS_DaLuus.FirstOrDefault(k => k.HoSoId == HoSoId && k.UserId == UserId);
            db.tbl_TD_HS_DaLuus.DeleteOnSubmit(temp);
            db.SubmitChanges();
        }
        public static IQueryable ViecLamLienQuan(int Tuyendungid,int nganhngheid)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var query =from a in db.tbl_TD_TuyenDungs.Where(k =>k.TuyenDungId != Tuyendungid &&(k.NganhNgheId1 == nganhngheid || k.NganhNgheId2 == nganhngheid || k.NgheNgheId3 == nganhngheid) && k.TinhTrang==1 && k.NgayHetHan >= DateTime.Now)
                       orderby a.NgayDang descending 
                       select new
                           {
                              a.TuyenDungId,
                              a.URL,
                              a.ChucDanh,
                           };
            return query.Take(15);
        }
    }
}